#!/usr/bin/python
import logging
import os
import random

import config.config
from libs.Config import Config


class Util:
    # 配置文件对象
    config = None

    def __init__(self):
        '初始化'
        # 初始化 配置文件
        self.config = Config()
        LOG_FORMAT = self.config.get('logs')['log_format']
        filedir = self.config.get('logs')['filedir']
        if filedir != '':
            if not os.path.exists(filedir):
                os.makedirs(filedir)
        filename = self.config.get('logs')['filename']
        logging.basicConfig(filename=filedir + filename,
                            level=logging.DEBUG, format=LOG_FORMAT)
        logging.info("Util Class Run.")

    def getRandomUserAgent(self):
        '获取一个随机的 user_agent'
        user_agent_list = self.config.get('user_agent_list')
        user_agent_list_len = len(user_agent_list)
        random_n = random.randint(0, user_agent_list_len)
        return user_agent_list[random_n]
